<?php
namespace Model\Skusystem;

use Library\Db;
use Library\Log;


/**
 * SkuModel
 * @author los_gsy
 */
class SkuModel extends BaseModel {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	static public function test() {
		return __METHOD__;
	}


	/**
	 * 获取最近更新的sku列表
	 * @param string $updatedAt
	 * @param int $page
	 * @param int $pagesize
	 * @return array	sku总数，每页sku列表
	 */
	static public function getSkuListByUpdatedAt($updatedAt, $page = 1, $pagesize = 20) {
		//定义变量
		$skuResult = array('cnt' => 0, 'list' => array());

		//查询sku列表
		//查询sku主表
		$db = new Db;
		$skuFieldList = array('sku', 'modifytime');
		$skuList = $db->withTotalCount()
			->where('isavailable', 1)
			->where('modifytime', $updatedAt, '>=')
// 			->where('sku', 'EA00201')
			->orderBy('modifytime', 'asc')
			->get('skus', array(($page - 1) * $pagesize, $pagesize), $skuFieldList);
		Log::debug(__METHOD__ . ' sql: ' . $db->getLastQuery());

		//如果查询出来的skuList为空，则直接返回
		if (!$skuList) {
			return $skuResult;
		}

		//查询sku属性表
		//组合sku code列表
		$skuCodeList = array();
		foreach ($skuList as $sku) {
			$skuCodeList[] = $sku['sku'];
		}

		//查询sku中文名
		$skuCodeCnnameList = self::getSkuCnnameListByCodeList($skuCodeList);

		//查询sku特殊物流属性，如粉末
		$skuCodeLogisticList = self::getSkuLogisticListByCodeList($skuCodeList);

		//查询sku成本价
		$skuCodeCostList = self::getSkuCostListByCodeList($skuCodeList);

		//查询sku重量
		$skuCodeWeightList = self::getSkuWeightListByCodeList($skuCodeList);

		//查询sku打包后的重量
		$skuCodePackWeightList = self::getSkuPackWeightListByCodeList($skuCodeList);

		//组合sku列表及其属性
		foreach ($skuList as &$sku) {
			$sku['name'] = isset($skuCodeCnnameList[$sku['sku']]) ? $skuCodeCnnameList[$sku['sku']] : 0;
			$sku['logistic'] = isset($skuCodeLogisticList[$sku['sku']]) ? $skuCodeLogisticList[$sku['sku']] : 0;
			$sku['cost'] = isset($skuCodeCostList[$sku['sku']]) ? $skuCodeCostList[$sku['sku']] : 0;
			$sku['weight'] = isset($skuCodeWeightList[$sku['sku']]) ? $skuCodeWeightList[$sku['sku']] : 0;
			$sku['packWeight'] = isset($skuCodePackWeightList[$sku['sku']]) ? $skuCodePackWeightList[$sku['sku']] : 0;
		}

		//Success
		$skuResult['cnt'] = $db->totalCount;
		$skuResult['list'] = $skuList;

		return $skuResult;
	}


	/**
	 * 通过sku code，查询sku中文名
	 * @param string $code
	 */
	static public function getSkuCnnameByCode($code) {
		//查询属性id
		$db = new Db;
		$nameId = $db->where('sku', $code)->getValue('sku_cnname', 'attr_id');

		//查询属性值
		$db2 = new Db;
		$name = $db2->where('id', $nameId)->getValue('cnname', 'name');

		//Success
		return $name;
	}


	/**
	 * 通过sku code列表，查询sku中文名列表
	 * @param array $code_list
	 */
	static public function getSkuCnnameListByCodeList($code_list) {
		//查询sku中文名属性id
		$db = new Db;
		$skuCnnameList = $db->where('sku', $code_list, 'in')->get('sku_cnname');

		//查询中文名属性值
		//取出sku cnname属性id
		$skuCnnameAttrIdList = array();
		foreach ($skuCnnameList as $skuCnname) {
			$skuCnnameAttrIdList[] = $skuCnname['attr_id'];
		}

		//do查询中文名属性值
		$db2 = new Db;
		$cnnameList = $db2->where('id', $skuCnnameAttrIdList, 'in')->get('cnname');

		//组合sku code与cnname的对应数组
		//组合cnname id与cnname value的对应数组
		$idCnnameList = array();
		foreach ($cnnameList as $cnname) {
			$idCnnameList[$cnname['id']] = $cnname['name'];
		}

		//do组合sku code与cnname的对应数组
		$skuCodeCnnameList = array();
		foreach ($skuCnnameList as $skuCnname) {
			$skuCodeCnnameList[$skuCnname['sku']] = isset($idCnnameList[$skuCnname['attr_id']]) ? $idCnnameList[$skuCnname['attr_id']] : '';
		}

		//Success
		return $skuCodeCnnameList;
	}


	/**
	 * 通过sku code列表，查询sku特殊物流属性列表
	 * @param array $code_list
	 */
	static public function getSkuLogisticListByCodeList($code_list) {
		//查询sku特殊物流属性id
		$db = new Db;
		$skuLogisticList = $db->where('sku', $code_list, 'in')->get('sku_logic');

		//查询sku特殊物流属性值
		//取出sku logistic属性id
		$skuLogisticAttrIdList = array();
		foreach ($skuLogisticList as $skuLogistic) {
			$skuLogisticAttrIdList[] = $skuLogistic['logic_attr'];
		}

		//如果sku logistic属性id列表不空，则直接返回空数组
		if (!$skuLogisticAttrIdList) {
			return array();
		}

		//do查询sku特殊物流属性值
		$db2 = new Db;
		$logisticList = $db2->where('id', $skuLogisticAttrIdList, 'in')->get('attr_logistic');

		//组合sku code与logistic的对应数组
		//组合logistic id与value的对应数组
		$idLogisticList = array();
		foreach ($logisticList as $logistic) {
			$idLogisticList[$logistic['id']] = $logistic['name'];
		}

		//do组合sku code与logistic的对应数组
		$skuCodeLogisticList = array();
		foreach ($skuLogisticList as $skuLogistic) {
			$skuCodeLogisticList[$skuLogistic['sku']] = isset($idLogisticList[$skuLogistic['logic_attr']]) ? $idLogisticList[$skuLogistic['logic_attr']] : '';
		}

		//Success
		return $skuCodeLogisticList;
	}


	/**
	 * 通过sku code列表，查询sku成本价
	 * @param array $code_list
	 */
	static public function getSkuCostListByCodeList($code_list) {
		//查询sku成本价
		$db = new Db;
		$skuCostList = $db->where('sku', $code_list, 'in')->get('price');

		//组合sku code与cost的对应数组
		$skuCodeCostList = array();
		foreach ($skuCostList as $skuCost) {
			$skuCodeCostList[$skuCost['sku']] = $skuCost['num'];
		}

		//Success
		return $skuCodeCostList;
	}


	/**
	 * 通过sku code列表，查询sku重量
	 * @param array $code_list
	 */
	static public function getSkuWeightListByCodeList($code_list) {
		//查询sku重量
		$db = new Db;
		$skuWeightList = $db->where('sku', $code_list, 'in')->get('weight');

		//组合sku code与weight的对应数组
		$skuCodeWeightList = array();
		foreach ($skuWeightList as $skuWeight) {
			$skuCodeWeightList[$skuWeight['sku']] = $skuWeight['num'];
		}

		//Success
		return $skuCodeWeightList;
	}


	/**
	 * 通过sku code列表，查询sku打包后的重量
	 * @param array $code_list
	 */
	static public function getSkuPackWeightListByCodeList($code_list) {
		//查询sku重量
		$db = new Db;
		$skuPackWeightList = $db->where('sku', $code_list, 'in')->get('asweight');

		//组合sku code与weight的对应数组
		$skuCodePackWeightList = array();
		foreach ($skuPackWeightList as $skuPackWeight) {
			$skuCodePackWeightList[$skuPackWeight['sku']] = $skuPackWeight['num'];
		}

		//Success
		return $skuCodePackWeightList;
	}





}